﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLog;
using Entity.Client;
using DAL;

public partial class User : System.Web.UI.Page
{
    #region Variables
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["EID"] != null)
            {
                switch (Request.QueryString["EID"])
                {
                    case "1001":
                        tr_RegisterUser.Style.Add("display", "");
                        tr_Login.Style.Add("display", "none");
                        spUserRegisterMessage.InnerHtml = "المستخدم موجود من قبل";
                        break;
                    case "1002":
                        tr_RegisterUser.Style.Add("display", "none");
                        tr_Login.Style.Add("display", "");
                        spUserLogin.InnerHtml = "اسم المستخدم او كلمة المرور خطأ";
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if (FormsFunction.GetLiveKoraCookie() == null)
                {
                    txtEmailAddress.Focus();
                }
                else
                {
                    Response.Redirect("Dashboard", false);
                }
            }
        }
        catch (Exception ex)
        {
            _logger.Error("User:::Page_Load:::" + ex.Message);
            throw;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            RegisterUsers objRegisterUsers = new RegisterUsers();
            RegUsers objRegUsers = new RegUsers();
            objRegisterUsers.EmailAddress = txtEmailAddress.Text;
            objRegisterUsers.Password = EncryptionMethods.Encryption.Encrypt(txtPassword.Text);
            int nUserID = objRegUsers.LoginClientUser(objRegisterUsers);
            if (nUserID >= 1)
            {
                FormsFunction.SetCookieValue(nUserID.ToString(), objRegisterUsers.EmailAddress);
                Response.Redirect("Dashboard", false);
            }
            else
            {
                spUserLogin.InnerHtml = "اسم المستخدم او كلمة المرور خطأ";
            }
        }
        catch (Exception ex)
        {
            _logger.Error("User:::btnSubmit_Click:::" + ex.Message);
            spUserLogin.InnerHtml = "هناك مشكلة في الدخول الى الحساب الرجاء المحاولة فيما بعد";
            throw;
        }
        tr_RegisterUser.Style.Add("display", "none");
        tr_Login.Style.Add("display", "");
    }
    protected void btnNewUser_Click(object sender, EventArgs e)
    {
        try
        {
            RegisterUsers objRegisterUsers = new RegisterUsers();
            RegUsers objRegUsers = new RegUsers();
            objRegisterUsers.AuthType = 1;
            objRegisterUsers.Country = 0;
            objRegisterUsers.EmailAddress = txtUserEmailAddress.Text;
            objRegisterUsers.FacebookID = "";
            objRegisterUsers.Password = EncryptionMethods.Encryption.Encrypt(txtUserPassword.Text);
            objRegisterUsers.UserFullName = txtUserFullName.Text;
            objRegisterUsers.UserStatus = 1;
            int nReturnUserID = objRegUsers.InsertNewRegisterUser(objRegisterUsers);
            if (nReturnUserID >=1)
            {
                FormsFunction.SetCookieValue(nReturnUserID.ToString(), objRegisterUsers.EmailAddress);
                spUserRegisterMessage.InnerHtml = "لقد تم تسجيل الحساب بنجاح <a href='Dashboard'>انقر هنا للدخول</a>";
                txtEmailAddress.Text = string.Empty;
                txtPassword.Text = string.Empty;
                txtUserEmailAddress.Text = string.Empty;
                txtUserFullName.Text = string.Empty;
                txtUserPassword.Text = string.Empty;
            }
            else
            {
                spUserRegisterMessage.InnerHtml = "المستخدم موجود من قبل";
            }
        }
        catch (Exception ex)
        {
            _logger.Error("User:::btnNewUser_Click:::" + ex.Message);
            spUserRegisterMessage.InnerHtml = "هناك مشكلة في تسجيل الحساب الرجاء المحاولة فيما بعد";
            throw;
        }
        tr_RegisterUser.Style.Add("display", "");
        tr_Login.Style.Add("display", "none");
    }
}